<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="项目名" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入项目名" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="开始日期">
				<el-date-picker v-model="daterangeStartDate" style="width: 240px" value-format="yyyy-MM-dd"
					type="daterange" range-separator="-" start-placeholder="开始日期"
					end-placeholder="结束日期"></el-date-picker>
			</el-form-item>
			<el-form-item label="结束日期">
				<el-date-picker v-model="daterangeEndDate" style="width: 240px" value-format="yyyy-MM-dd"
					type="daterange" range-separator="-" start-placeholder="开始日期"
					end-placeholder="结束日期"></el-date-picker>
			</el-form-item>
			<el-form-item label="项目状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择项目状态" clearable filterable>
					<el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
						:value="dict.dictValue" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="handleQuery">搜索</el-button>
				<el-button @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain :disabled="single" @click="handleUpdate"
					v-hasPermi="['baseInfo:projectInfo:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain :disabled="multiple" @click="handleDelete"
					v-hasPermi="['baseInfo:projectInfo:remove']">删除</el-button>
			</el-col>

		</el-row>

		<el-table v-loading="loading" :data="staticData" @selection-change="handleSelectionChange">
			<el-table-column type="selection" align="center" />
			<el-table-column label="序号" align="center" prop="id" />
			<el-table-column label="名称" align="center" prop="name" width="200" />
			<el-table-column label="编码" align="center" prop="ids" width="200" />
			<el-table-column label="预警时间" align="center" prop="warningTime" width="200">
				<!-- <template #default="scope">
					<span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
				</template> -->
			</el-table-column>
			<!-- <el-table-column label="结束日期" align="center" prop="endDate" width="200">
				<template #default="scope">
					<span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column> -->
			<el-table-column label="传感器名称" align="center" prop="transducer" width="200" />
			<el-table-column label="传感器数据" align="center" prop="sensorData" width="200" />
			<el-table-column label="接单时间" align="center" prop="acceptanceTime" width="200">
				<!-- <template #default="scope">
					<span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
				</template> -->
			</el-table-column>
			<el-table-column label="处理人员" align="center" prop="user" width="200" />
			<el-table-column label="处理状态" align="center" prop="userStatus" width="200" />
			<el-table-column label="处理时间" align="center" prop="userTime" width="200">
				<!-- <template #default="scope">
					<span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
				</template> -->
			</el-table-column>
			<el-table-column label="预警级别" align="center" prop="level"  width="200" />
			<el-table-column label="备注" align="center" prop="trips" width="200" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
				<template #default="scope">
					<el-button type="text" @click="handleUpdate(scope.row)"
						v-hasPermi="['baseInfo:projectInfo:edit']">修改</el-button>
					<el-button type="text" @click="handleDelete(scope.row)"
						v-hasPermi="['baseInfo:projectInfo:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改项目维护对话框 -->
		<el-dialog v-if="open" :title="title" v-model="open" width="600px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="项目名" prop="name">
					<el-input v-model="form.name" placeholder="请输入项目名" />
				</el-form-item>
				<el-form-item label="负责人" prop="leader">
					<el-input v-model="form.leader" placeholder="请输入负责人" />
				</el-form-item>
				<el-form-item label="开始日期" prop="startDate">
					<el-date-picker clearable v-model="form.startDate" type="date" value-format="yyyy-MM-dd"
						placeholder="选择开始日期">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="结束日期" prop="endDate">
					<el-date-picker clearable v-model="form.endDate" type="date" value-format="yyyy-MM-dd"
						placeholder="选择结束日期">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="项目状态" prop="status">
					<el-select v-model="form.status" placeholder="请选择项目状态" clearable filterable>
						<el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
							:value="dict.dictValue"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="项目介绍">
					<editor v-model="form.description" :min-height="192" />
				</el-form-item>
			</el-form>
			<template #footer class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listProjectInfo,
		getProjectInfo,
		delProjectInfo,
		addProjectInfo,
		updateProjectInfo,
		exportProjectInfo
	} from "@/api/baseInfo/projectInfo";
	import Editor from '@/components/MyEditor';

	export default {
		name: "ProjectInfo",
		components: {
			Editor,
		},
		data() {
			return {
				// 遮罩层
				loading: true,
				staticData:[{
					id:'01',
					name:'002车厢',
					ids:'61269',
					warningTime:'2024-10-10',
					transducer:'01',
					sensorData:'2',
					acceptanceTime:'2024-10-11' ,
					user:'002',
					userStatus:'处理中',
					userTime:'2024-10-13',
					level:'一级',
					trips:'无'
				},{
					id:'02',
					name:'005车厢',
					ids:'45943',
					warningTime:'2024-10-10',
					transducer:'03',
					sensorData:'4',
					acceptanceTime:'2024-10-11' ,
					user:'001',
					userStatus:'处理中',
					userTime:'2024-10-13',
					level:'一级',
					trips:'无'
				},{
					id:'03',
					name:'006车厢',
					ids:'64760',
					warningTime:'2024-10-10',
					transducer:'04',
					sensorData:'3',
					acceptanceTime:'2024-10-11' ,
					user:'001',
					userStatus:'处理中',
					userTime:'2024-10-13',
					level:'一级',
					trips:'无'
				},{
					id:'04',
					name:'009车厢',
					ids:'16564',
					warningTime:'2024-10-10',
					transducer:'06',
					sensorData:'6',
					acceptanceTime:'2024-10-11' ,
					user:'003',
					userStatus:'处理中',
					userTime:'2024-10-13',
					level:'一级',
					trips:'无'
				},{
					id:'05',
					name:'011车厢',
					ids:'60427',
					warningTime:'2024-10-10',
					transducer:'02',
					sensorData:'1',
					acceptanceTime:'2024-10-11' ,
					user:'003',
					userStatus:'处理中',
					userTime:'2024-10-13',
					level:'一级',
					trips:'无'
				},{
					id:'06',
					name:'015车厢',
					ids:'24392',
					warningTime:'2024-10-10',
					transducer:'05',
					sensorData:'9',
					acceptanceTime:'2024-10-11' ,
					user:'005',
					userStatus:'处理中',
					userTime:'2024-10-13',
					level:'二级',
					trips:'无'
				},{
					id:'07',
					name:'018车厢',
					ids:'9721',
					warningTime:'2024-10-11',
					transducer:'01',
					sensorData:'4',
					acceptanceTime:'2024-10-12' ,
					user:'007',
					userStatus:'处理中',
					userTime:'2024-10-14',
					level:'一级',
					trips:'无'
				},{
					id:'08',
					name:'019车厢',
					ids:'92655',
					warningTime:'2024-10-11',
					transducer:'03',
					sensorData:'5',
					acceptanceTime:'2024-10-12' ,
					user:'005',
					userStatus:'处理中',
					userTime:'2024-10-14',
					level:'一级',
					trips:'无'
				},{
					id:'09',
					name:'023车厢',
					ids:'85464',
					warningTime:'2024-10-11',
					transducer:'06',
					sensorData:'1',
					acceptanceTime:'2024-10-12' ,
					user:'002',
					userStatus:'处理中',
					userTime:'2024-10-14',
					level:'一级',
					trips:'无'
				},{
					id:'10',
					name:'025车厢',
					ids:'68192',
					warningTime:'2024-10-11',
					transducer:'05',
					sensorData:'2',
					acceptanceTime:'2024-10-12' ,
					user:'001',
					userStatus:'处理中',
					userTime:'2024-10-14',
					level:'一级',
					trips:'无'
				},{
					id:'11',
					name:'026车厢',
					ids:'47968',
					warningTime:'2024-10-11',
					transducer:'02',
					sensorData:'5',
					acceptanceTime:'2024-10-12' ,
					user:'003',
					userStatus:'处理中',
					userTime:'2024-10-14',
					level:'一级',
					trips:'无'
				},{
					id:'12',
					name:'028车厢',
					ids:'77614',
					warningTime:'2024-10-11',
					transducer:'04',
					sensorData:'7',
					acceptanceTime:'2024-10-12' ,
					user:'006',
					userStatus:'处理中',
					userTime:'2024-10-14',
					level:'一级',
					trips:'无'
				},],
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 项目维护表格数据
				projectInfoList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 开始日期时间范围
				daterangeStartDate: [],
				// 结束日期时间范围
				daterangeEndDate: [],
				// 项目状态字典
				statusOptions: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: null,
					startDate: null,
					endDate: null,
					status: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					name: [{
							required: true,
							message: "项目名不能为空",
							trigger: "blur"
						},
						{
							max: 20,
							message: "项目名最多20个字符",
							trigger: "blur"
						}
					],
					leader: [{
							required: true,
							message: "负责人不能为空",
							trigger: "blur"
						},
						{
							max: 50,
							message: "负责人最多50个字符",
							trigger: "blur"
						}
					],
					startDate: [{
						required: true,
						message: "开始日期不能为空",
						trigger: "blur"
					}],
					endDate: [{
						required: true,
						message: "结束日期不能为空",
						trigger: "blur"
					}],
				}
			};
		},
		created() {
			this.getList();
			this.getDicts("project_status").then(response => {
				this.statusOptions = response.data;
			});
		},
		methods: {
			/** 查询项目维护列表 */
			getList() {
				this.loading = true;
				this.queryParams.params = {};
				if (null != this.daterangeStartDate && '' != this.daterangeStartDate) {
					this.queryParams.params["beginStartDate"] = this.daterangeStartDate[0];
					this.queryParams.params["endStartDate"] = this.daterangeStartDate[1];
				}
				if (null != this.daterangeEndDate && '' != this.daterangeEndDate) {
					this.queryParams.params["beginEndDate"] = this.daterangeEndDate[0];
					this.queryParams.params["endEndDate"] = this.daterangeEndDate[1];
				}
				listProjectInfo(this.queryParams).then(response => {
					this.projectInfoList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 项目状态字典翻译
			statusFormat(row, column) {
				return this.selectDictLabel(this.statusOptions, row.status);
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					name: null,
					leader: null,
					startDate: null,
					endDate: null,
					description: null,
					status: null,
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.daterangeStartDate = [];
				this.daterangeEndDate = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加项目维护";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getProjectInfo(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改项目维护";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateProjectInfo(this.form).then(response => {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addProjectInfo(this.form).then(response => {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$confirm('是否确认删除项目维护编号为"' + ids + '"的数据项?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return delProjectInfo(ids);
				}).then(() => {
					this.getList();
					this.msgSuccess("删除成功");
				})
			},
			/** 导出按钮操作 */
			handleExport() {
				const queryParams = this.queryParams;
				this.$confirm('是否确认导出所有项目维护数据项?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return exportProjectInfo(queryParams);
				}).then(response => {
					this.download(response.msg);
				})
			}
		}
	};
</script>